/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.contorller;

import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.portal.common.restfull.RequestMapping;
import com.apache.portal.common.restfull.ResultFullAnntation;
import com.apache.portal.common.restfull.SupperAction;
import com.apache.portal.common.template.FreemarkerHelper;
import com.apache.portal.common.util.FileOperate;
import com.apache.portal.common.util.PortalUtil;
import com.apache.tools.DateUtils;
import com.apache.tools.MoneyUtil;
import com.apache.uct.common.ToolsUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  add 2017-06-13
 */
@SuppressWarnings("serial")
@ResultFullAnntation(name = "printAction", urlPatterns = { "/common/print/*" })
public class PrintAction extends SupperAction {

    private static Logger log = Logger.getLogger(PrintAction.class);

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     */
    protected void doInvoke(HttpServletRequest request, HttpServletResponse response,
            String methodType) throws Exception {
        Method method = doInvoke(this.getClass(), request, response, methodType, "");
        if (null != method) {
            Object obj = method.invoke(this, request, response);
            if (!ToolsUtil.isEmpty(obj)) {
                JSONObject array = JSONObject.fromObject(obj);
                this.outputJson(array.toString(), response, request.getParameter("callback"));
            }
        } else {
            gotoErrorPage(request, response, "请求的Action地址未定义");
        }
    }

    @RequestMapping(value = "cteateDoc", method = "post")
    public ResultMsg cteateDoc(HttpServletRequest request, HttpServletResponse response) {
        ResultMsg msg = new ResultMsg("F", "文档创建失败");
        //		String sysEname = request.getParameter("sysEname");
        //		String sysCname = request.getParameter("sysCname");
        String root_path = ToolsUtil.getInstance()
                .getValueByKey("UPLOAD_DIRECTORY");//根目录  E:/upload
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            String proName = request.getParameter("proName");
            String tempFile = request.getParameter("tempFile");
            data.put("year", DateUtils.format(new Date(), "yyyy"));
            data.put("month", DateUtils.format(new Date(), "MM"));
            data.put("day", DateUtils.format(new Date(), "dd"));
            data.put("proType", request.getParameter("proType"));
            data.put("proName", proName);
            data.put("contractNo", request.getParameter("contractNo"));
            data.put("proCount", Validator.getDefaultStr(request.getParameter("proCount"), "0"));
            data.put("contractPrice",
                    Validator.getDefaultStr(request.getParameter("contractPrice"), ""));
            data.put("sellerCname",
                    Validator.getDefaultStr(request.getParameter("sellerCname"), ""));
            data.put("userCname", Validator.getDefaultStr(request.getParameter("userCname"), ""));
            data.put("contractPriceMax", MoneyUtil.CNValueOf(
                    Validator.getDefaultStr(request.getParameter("contractPrice"), "0")));
            String printNo = PortalUtil.getInstance().createSeqNo("print", "3");
            printNo = printNo.replace("print", "").trim();
            data.put("conNum", printNo);

            char[] no = printNo.toCharArray();
            String chNo = "";
            for (char c : no) {
                if (Validator.isNull(chNo)) {
                    chNo = getCH(c - 48);
                } else {
                    chNo += getCH(c - 48);

                }
            }

            data.put("conNumMax", chNo);

            System.out.println(data);
            String tempPath = "coding/pages/zjsContract/jinan-contract.xml";

            //获取模板填充后的内容
            String content = FreemarkerHelper.instance().getTemplateStr(data, tempPath);
            String fileName = proName + ".doc";
            //生成文件路径
            String filePath = "/atta/print/" + fileName;
            //生成文件的全路径
            String path = root_path + filePath;
            //存放文件的目录（不存在就创建，存在就不创建）
            FileOperate.getInstance().newCreateFolder(root_path + "/atta/print");
            //创建文件。
            boolean flag2 = FileOperate.getInstance().newCreateFile(path, content);
            if (flag2) {
                log.info("文件生成成功：" + filePath);
                Map<String, String> resultMap = new HashMap<String, String>();
                resultMap.put("fileName", fileName);
                resultMap.put("filePath", filePath);
                msg = new ResultMsg("T", JSONObject.fromObject(resultMap).toString());
            } else {
                msg = new ResultMsg("F", "生成失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    private String getCH(int input) {
        String sd = "";
        switch (input) {
        case 0:
            sd = "零";
            break;
        case 1:
            sd = "一";
            break;
        case 2:
            sd = "二";
            break;
        case 3:
            sd = "三";
            break;
        case 4:
            sd = "四";
            break;
        case 5:
            sd = "五";
            break;
        case 6:
            sd = "六";
            break;
        case 7:
            sd = "七";
            break;
        case 8:
            sd = "八";
            break;
        case 9:
            sd = "九";
            break;
        default:
            break;
        }
        return sd;
    }
}
